<template>
	<view class="login_section" :style="{ 'min-height': `${change_var.win_height}rpx` }">
		<block v-if="page_model == 'section'">
			<view
				class="page_login page_loading page_login_gradient"
				:class="{
					can_show: page_render,
					wechat: page_handle_model == 'wechat_login',
				}"
			>
				<diynav
					ref="diynav"
					:navstyle="{ background: 'transparent' }"
					isFixed
					hasHeight
					:title="page_tt[page_handle_model].nav_title"
					:back="page_tt[page_handle_model].show_back"
					colorModel="day"
					:backfun="true"
					:pagename="page_tt[page_handle_model].nav_title"
					@settop="page_show"
					@topback="topback"
				></diynav>

				<view class="login_main">
					<view class="wechat_login" v-if="page_handle_model == 'wechat_login'">
						<view class="wechat_step">
							<block v-if="page_step == 1">
								<view class="wechat_step_top">
									<view class="now_page">01</view>
									<view class="line">/</view>
									<view class="total_page">02</view>
								</view>
								<view class="wechat_step_center">
									<image class="imgs" src="/static/images/wechat_login.png" mode="widthFix"></image>
									<view class="tt">
										<view class="t1">手机号快捷登录</view>
										<view class="t2">同意即获取微信手机号</view>
									</view>
								</view>
								<view class="wechat_step_btns">
									<button @click="topback" class="btn">拒绝</button>
									<button v-if="ystk" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" class="btn">同意</button>
									<button v-else @click="login_now()" class="btn">同意</button>
								</view>
							</block>

							<block v-if="page_step == 2">
								<view class="wechat_step_avatar_box">
									<button class="wechat_step_avatar" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
										<image class="avatar" :src="avatarUrl ? check_pic_all(avatarUrl) : '/static/images/login_no.png'" mode="aspectFill"></image>
									</button>
									<view class="avatar_tt">
										<text class="red">*</text>
										点击图标上传头像
									</view>
								</view>

								<view class="daninput_box">
									<view class="text-k jj_kr_a">
										<text class="red">*</text>
										昵称
									</view>
									<input :adjust-position="false" type="nickname" class="daninput" v-model="nickname" placeholder="请输入昵称" />
								</view>

								<view class="wechat_step_btns">
									<button @click="topback()" class="btn">跳过</button>
									<button @click="update_user()" class="btn">保存</button>
								</view>
							</block>
						</view>
					</view>

					<view class="login_view" v-else>
						<view class="page_step_1">
							<view class="page_logo" v-if="page_tt[page_handle_model].show_logo">
								<image class="img" src="/static/images/logo_w.png"></image>
							</view>

							<view class="login_form">
								<view class="login_form_top">
									{{ page_tt[page_handle_model].tt }}
								</view>

								<view class="login_form_view">
									<block v-for="(v, i) in page_tt[page_handle_model].form_view" :key="i">
										<view class="form_inp" v-if="v.type == 'phone'">
											<view class="area_code">+86</view>
											<input
												:adjust-position="false"
												class="inp"
												:type="v.inp_type"
												:placeholder="v.placeholder"
												placeholder-class="placeholder"
												v-model="login_form[v.key]"
											/>
										</view>

										<view class="form_inp" v-if="v.type == 'code'">
											<input
												:adjust-position="false"
												class="inp"
												:type="v.inp_type"
												:placeholder="v.placeholder"
												placeholder-class="placeholder"
												v-model="login_form[v.key]"
												maxlength="6"
											/>
											<view class="send_code" @click="send_code">
												{{ code_tips }}
											</view>
										</view>

										<view class="form_inp" v-if="v.type == 'pwd'">
											<view class="pwd_icon">
												<image class="img" src="/static/images/pwd.png"></image>
											</view>

											<input
												:adjust-position="false"
												class="inp"
												:type="v.inp_type"
												:placeholder="v.placeholder"
												placeholder-class="placeholder"
												v-model="login_form[v.key]"
											/>
										</view>

										<view class="form_inp" v-if="v.type == 'con_pwd'">
											<view class="pwd_icon">
												<image class="img" src="/static/images/pwd.png"></image>
											</view>
											<input
												:adjust-position="false"
												class="inp"
												:type="v.inp_type"
												:placeholder="v.placeholder"
												placeholder-class="placeholder"
												v-model="login_form[v.key]"
											/>
										</view>
									</block>
								</view>
							</view>

							<button @click="btn_click" class="login_btn" :loading="loading == 1">
								{{ page_tt[page_handle_model].btn_tt }}
							</button>

							<view class="btn_tips" :class="{ isbet: page_handle_model == 'pwd_login' }">
								<block v-if="page_handle_model == 'yzm_login'">
									<!-- <view class="tips_tt" @click="chang_model('pwd_login')">
										密码登录
									</view> -->
								</block>
								<block v-else-if="page_handle_model == 'pwd_login'">
									<view class="tips_tt" @click="chang_model('yzm_login')">验证码登录</view>
									<view class="tips_tt" @click="chang_model('forget_pwd')">忘记密码?</view>
								</block>
							</view>
						</view>
					</view>

					<view class="check_ystk" v-if="page_tt[page_handle_model].show_ystk">
						<myVerifyPopup :on="verifyshow" verifytips="请先同意以下协议"></myVerifyPopup>
						<view class="ystk_view" @click="com_change_var('ystk')">
							<mycheckbox :checkon="ystk" width="26" padding="0" @check_click="com_change_var('ystk')"></mycheckbox>

							<text class="txt_show">我已阅读并同意笆林</text>
							<text class="b" @click.stop="showrule('base_user_agreement')">《用户协议》</text>
							和
							<text class="b" @click.stop="showrule('base_privacy_policy')">《隐私协议》</text>
						</view>
					</view>
				</view>
			</view>
		</block>

		<block v-if="page_model == 'bind_device'">
			<view class="bind_adf_swiper_view" v-if="objJudge(bind_adf_swiper)">
				<myswiper
					:LIST="bind_adf_swiper"
					dotClass="bind_adf_dots"
					circular
					autoplay
					:interval="4000"
					:duration="800"
					width="750"
					:height="change_var.win_height"
					radius="0"
					imgClickMode="link"
				></myswiper>
				<view class="bind_btn" @click="bind_device_fun">绑定我的设备</view>
			</view>
		</block>

		<block v-if="page_model == 'page'">
			<slot></slot>
		</block>
	</view>
</template>

<script>
import { common } from '@/util/method.js';
import storeMixin from '@/store/storeMixin';
export default {
	mixins: [storeMixin],
	components: {},
	name: 'pagelogin',
	props: {
		backurl: {
			type: String,
			default: '',
		},
		// 页面是否必须用户登陆
		need_login: {
			type: [Number, String],
			default: 1,
		},
		// 页面需要校验绑定车辆
		need_car: {
			type: [Number, String],
			default: 0,
		},

		p_model: {
			type: String,
			default: 'yzm_login',
		},
	},
	data() {
		return {
			change_var: {},
			page_render: false,
			scrollTop: 0,

			// bind_device 设备绑定 section 组件登录逻辑  page 插槽页面
			page_model: 'page',
			/*
			1.验证码登录 yzm_login
			2.密码登录 pwd_login
			3.设置密码 set_pwd
			4.忘记密码 forget_pwd
			5.修改密码 edit_pwd
			5.小程序登陆 按钮获取手机号 wechat_login
			*/
			page_handle_model: this.p_model,
			page_tt: {
				yzm_login: {
					tt: '验证码登录',
					nav_title: '',
					btn_tt: '登录',
					show_back: false,
					show_ystk: true,
					show_logo: true,
					form_view: [
						{
							inp_type: 'number',
							type: 'phone',
							placeholder: '请输入11位手机号码',
							key: 'phone',
						},
						{
							inp_type: 'number',
							type: 'code',
							placeholder: '请输入验证码',
							key: 'code',
						},
					],
				},
				pwd_login: {
					tt: '密码登录',
					nav_title: '',
					btn_tt: '登录',
					show_back: false,
					show_ystk: true,
					show_logo: true,
					form_view: [
						{
							inp_type: 'number',
							type: 'phone',
							placeholder: '请输入11位手机号码',
							key: 'phone',
						},
						{
							inp_type: 'password',
							type: 'pwd',
							placeholder: '请输入密码',
							key: 'pwd',
						},
					],
				},
				set_pwd: {
					tt: '设置密码',
					nav_title: '',
					btn_tt: '确认',
					show_back: true,
					show_ystk: false,
					show_logo: false,
				},
				forget_pwd: {
					tt: '忘记密码',
					nav_title: '',
					btn_tt: '确认',
					show_back: true,
					show_ystk: false,
					show_logo: false,
					form_view: [
						{
							type: 'phone',
							inp_type: 'number',
							placeholder: '请输入11位手机号码',
							key: 'phone',
						},
						{
							type: 'code',
							inp_type: 'number',

							placeholder: '请输入验证码',
							key: 'code',
						},
						{
							type: 'pwd',
							inp_type: 'password',

							placeholder: '请输入密码',
							key: 'pwd',
						},
						{
							type: 'con_pwd',
							inp_type: 'password',
							placeholder: '请确认密码',
							key: 'con_pwd',
						},
					],
				},
				edit_pwd: {
					tt: '修改密码',
					nav_title: '',
					btn_tt: '确认',
					show_back: true,
					show_ystk: false,
					show_logo: false,
					form_view: [
						{
							type: 'pwd',
							inp_type: 'password',

							placeholder: '请输入密码',
							key: 'pwd',
						},
						{
							type: 'con_pwd',
							inp_type: 'password',
							placeholder: '请确认密码',
							key: 'con_pwd',
						},
					],
				},

				wechat_login: {
					tt: '微信登陆',
					nav_title: '登录',
					btn_tt: '登录',
					show_back: true,
					show_ystk: true,
					show_logo: true,
					form_view: [
						{
							inp_type: 'number',
							type: 'phone',
							placeholder: '请输入11位手机号码',
							key: 'phone',
						},
						{
							inp_type: 'number',
							type: 'code',
							placeholder: '请输入验证码',
							key: 'code',
						},
					],
				},
			},
			page_step: 1, //登录步骤  微信登陆用
			page_need_login: Number(this.need_login),
			page_check_car: Number(this.need_car),
			loading: 0,
			login_form: {
				phone: '',
				code: '',
				pwd: '',
			},

			code_tips: '获取验证码',
			send_req: true,
			send_code_flag: false, //发送过验证码
			iTime: 59,
			iTime_task: null,

			ystk: false,

			reqone: false,

			wx_code: null,
			avatarUrl: '',
			nickname: '',
			nextreq: true,

			bind_adf_swiper: [],

			check_privacy_title: '',
			check_privacy_rich: '',
			verifyshow: false,

			init_task: null,
		};
	},
	beforeCreate() {},
	created() {
		this.init_pagedata('pagelogin');
	},
	destroyed() {},
	mounted() {
		this.pagelogin_judge_render();
	},
	methods: {
		...common,
		page_show(e, n) {
			let _this = this;
			// console.log(this, n, 'set_pagetop')
			_this.page_render = false;
			_this.change_var.page_top = e.height;
			_this.page_render = true;
			_this.module_judge_render();
		},

		pagelogin_judge_render() {
			// 父组件有 diynav mybtns 通过 module_judge_render 中判断组件内部的render值是否都为1 才手动执行 component_init，
			// 不建议自动执行页面会错乱，因为page_model 的切换导致diynav组件还没来得及计算就给移除了 但是又不触发销毁重建生命周期
			let p_refs = {};

			if (this.objJudge(this.$parent)) {
				p_refs = this.$parent.$refs;
			}

			let filter_ref = ['diynav', 'mybtns'];
			let n = 0;

			// console.log(this)
			if (this.objJudge(p_refs)) {
				filter_ref.forEach(item => {
					if (p_refs[item]) {
						n++;
					}
				});
			}
			if (n == 0) {
				// 没有异步计算的才自动执行
				this.component_init();
			}
		},

		async component_init() {
			let _this = this;
			let islogin = false,
				// is_chekc_privacy = uni.getStorageSync('agree_privacy') || false,
				isBind = false;
			// if (!is_chekc_privacy) {
			// 	_this.privacy_init();
			// 	_this.page_model = 'check_privacy';
			// 	return;
			// }

			islogin = await _this.getusertoken();
			// if (islogin && _this.page_check_car == 1) {
			// 	isBind = await _this.get_mycarlist();
			// }

			if (_this.p_model == 'wechat_login') {
			} else if (_this.p_model != 'yzm_login') {
				_this.call_chang_model(_this.p_model);
			}
			// console.log({
			// 	islogin,
			// 	isBind,
			// });
			_this.$emit('initcallback');
			_this.config_init();
			switch (_this.page_need_login) {
				case 1:
					// 必须要登录才展示的页面
					if (islogin) {
						// 已登录
						if (_this.page_check_car == 1) {
							if (isBind) {
								uni.setNavigationBarColor({
									frontColor: '#000000',
									backgroundColor: '#000000',
								});
								// console.log('isbind');
								// 已绑定
								_this.page_model = 'page';
								_this.$emit('loginready');
							} else {
								// console.log('isbindfalse');
								// debugger;
								_this.adf_init();
								// 未绑定设备
								uni.setNavigationBarColor({
									frontColor: '#ffffff',
									backgroundColor: '#ffffff',
								});
								_this.page_model = 'bind_device';
							}
						} else {
							_this.page_model = 'page';
							_this.$emit('loginready');
						}
					} else {
						// 未登录
						// tabbar页面会有底部模块导致页面未占满
						// 跳转到中转页login进行登录  login页可以占满一屏高
						if (_this.isLogin_page()) {
							// login页才能登陆
							_this.page_model = 'section';
							_this.req_init();
						} else {
							// 统一跳转到login页
							_this.goweburl_f('/pagesA/index/login');
						}
					}
					break;
				case 0:
					// 非必要登陆
					_this.page_model = 'page';
					_this.$emit('loginready');
					break;
			}
		},

		async get_mycarlist() {
			let _this = this;
			let { state = 0, data = {} } = await _this.com_getdata({
				url: '/cc/list',
				id_name: 'user_id',
			});
			if (state == 1) {
				if (_this.objJudge(data)) {
					let _car = (await _this.init_car_list(data)) ? true : false;
					return _car;
				} else {
					return false;
				}
			} else {
				return false;
			}
		},
		// 初始化车辆信息
		async init_car_list(list) {
			let _this = this;
			// ---------------------
			let new_list = [];
			let bike_cc_id = uni.getStorageSync('bike_cc_id') || false;
			list.forEach(car => {
				for (const key in car) {
					let value = car[key];
					if (!_this.objJudge(value)) {
						if (key == 'bike_cover') {
							car[key] = _this.COMMON_VAR.default_img.car_cover;
						} else if (key == 'bike_image') {
							car[key] = _this.COMMON_VAR.default_img.car_img;
						} else {
							car[key] = '--';
						}
					}
				}
				car['car_show_name'] = car.bike_cc_id;
				if (_this.objJudge(car.bike_sn)) {
					car['car_show_name'] = car.bike_sn;
				}
				if (_this.objJudge(car.bike_name)) {
					car['car_show_name'] = car.bike_name;
				}
				if (car.bike_cc_id == bike_cc_id) {
					new_list.unshift(car);
				} else {
					new_list.push(car);
				}
			});
			// ---------------------
			if (_this.objJudge(new_list)) {
				_this.store_set_state({ state_name: 'CAR_MYLIST', state_value: new_list });

				let _info = {};
				if (_this.objJudge(bike_cc_id)) {
					let RECORD = new_list.find(item => {
						return item.bike_cc_id == bike_cc_id;
					});
					Object.assign(_info, RECORD);
				} else {
					Object.assign(_info, new_list[0]);
				}
				let {
					state = 0,
					data = {},
					battery = {},
					xuhang = '--',
				} = await _this.com_getdata({
					url: `/cc/info`,
					data: {
						bike_cc_id: _info.bike_cc_id,
					},
				});
				if (state == 1) {
					let _battery = {};
					if (_this.objJudge(battery)) {
						_battery = battery;
						_info['has_battery'] = true;
					}
					_info['xuhang'] = xuhang;
					Object.assign(_info, data, _battery);

					_info.refresh_time = _this.happenTimeFun(data.last_online_time, 'y/m/d h:i:s');
					let min = 15;
					let start_t = _this.happenTimeFun(new Date().getTime(), 'y-m-d');
					let end_t = _this.happenTimeFun(data.remote_expire_time, 'y-m-d');
					let hasday = _this.compare_time(start_t, end_t, 'day');
					_info['expire_day'] = hasday;

					let expire_tips = `远程服务将于${hasday}天后过期，请续费！`;

					if (hasday == 0) {
						expire_tips = `远程服务即将过期，请续费！`;
					} else if (hasday == -1) {
						expire_tips = `远程服务已过期，请续费！`;
					}
					_info['min_expire_day'] = min;
					_info['expire_tips'] = expire_tips;
					_info['expire_time'] = end_t;

					if (hasday <= min && _this.objJudge(_info.bike_battery_id)) {
						_info['show_server_tips'] = true;
					} else {
						_info['show_server_tips'] = false;
					}

					// console.log(start_t, end_t, hasday)

					if (!_this.objJudge(_info.dev_capacity)) {
						_info['dev_capacity'] = '--';
					}

					if (!_this.objJudge(_info.dev_xuhang)) {
						_info['dev_xuhang'] = '--';
					}

					if (!_this.objJudge(_info.bike_vin)) {
						_info['bike_vin'] = '--';
					} else {
						_info['bike_vin'] = _info.bike_vin / 1000;
					}

					if (!_this.objJudge(_info.bike_cover)) {
						_info['bike_cover'] = _this.COMMON_VAR.default_img.car_cover;
					}

					if (!_this.objJudge(_info.controller_mode_name)) {
						_info['controller_mode_name'] = '暂无模式';
					}

					_info['bike_status'] = 1;

					_info['pre_show'] = _this.back_number(_info.dev_soc, 0);

					_info['car_show_type'] = '电量';
					_info['car_show_type_icon'] = '/static/images/ele_icon.png';
					_info['car_show_type_unit'] = '%';
					_info['car_show_type_value'] = _info.dev_soc;
					_info['bike_vin_max'] = parseFloat(_info.bike_battery_mode) || 70;

					if (!_this.objJudge(_info.bike_sn)) {
						// 识别不到车辆
						_info['bike_status'] = -1;
					}

					if (!_this.objJudge(_info.bike_battery_id) || _info.bike_battery_id == '0000') {
						// 大家厂家的电池 没有id
						// 识别不到电池
						// 其他电池 有电压
						// bike_vin
						if (_this.objJudge(_info.bike_vin)) {
							_info['bike_status'] = -2;

							_info['car_show_type'] = '电压';
							_info['car_show_type_icon'] = '/static/images/v_icon.png';
							_info['car_show_type_unit'] = 'V';
							_info['car_show_type_value'] = _info.bike_vin;
							_info['pre_show'] = _this.back_number((_info.bike_vin / _info.bike_vin_max) * 100, 0);
						}
					}
					if (_info.online == '1') {
						// 已离线
						_info['pre_show'] = '--';
						_info['car_show_type_value'] = '--';
					}
					uni.setStorageSync('bike_cc_id', _info.bike_cc_id);
					uni.setStorageSync('bike_battery_id', _info.bike_battery_id);
					uni.setStorageSync('bike_battery_mode', _info.bike_battery_mode);
					uni.setStorageSync('controller_mode_id', _info.controller_mode_id);
					_this.UPDATE_STATE_CARINFO(_info);
					return _info;
				} else {
					return false;
				}
			}
		},

		// 主动登录
		page_login() {
			let _this = this;
			_this.page_need_login = 1;
			_this.component_init();
		},
		req_init() {
			// 隐私条款 用户协议
			return;
		},

		// 绑定设备
		bind_device_fun() {
			let _this = this;
			// _this.com_bind_menu()
			_this.goweburl_f('/pagesB/index/user_bind_scan');
		},
		config_init() {
			let _this = this;
		},

		privacy_init() {
			let _this = this;
			_this.com_getdata({
				url: '/base/config',
				data: {
					config_key: 'base_privacy_policy_check',
				},
				data_fun: function (res) {
					if (res.state == 1) {
						_this.check_privacy_title = res.info.config_remark;
						_this.check_privacy_rich = res.info.config_value;
					}
					_this.com_loading_fun({ mode: 0 });
				},

				fail_fun: function (res) {},
			});
		},
		async adf_init() {
			let _this = this;
			let { state = 0, data = [] } = await _this.com_getdata({
				url: '/banner/list',
				data_handle_on: false,
				method: 'GET',
				data: {
					pos: 'APP绑定页',
				},
			});

			if (state == 1) {
				_this.bind_adf_swiper = data;
			}
		},
		isLogin_page() {
			let routes = getCurrentPages();
			let route = routes[routes.length - 1].route; // 页面栈中的最后一个项为当前页面，route属性为页面路径
			let tabbar = ['pagesA/index/login'];
			return tabbar.indexOf(route) > -1 ? true : false;
		},
		check_ystk_fun() {
			let _this = this;
			if (!_this.ystk) {
				if (_this.verifyshow) return false;
				_this.verifyshow = true;
				setTimeout(() => {
					_this.verifyshow = false;
				}, 3000);
				return false;
			} else {
				return true;
			}
		},
		async send_code() {
			let _this = this;
			if (!_this.check_ystk_fun()) return;
			if (!_this.page_check(_this.login_form.phone, 'phone')) return;
			if (!_this.com_loading({ status: 1, k: 'send_req', title: '发送中...', throttle: true })) return;
			let data = {
				phone: _this.login_form.phone,
				type: 'register',
			};
			if (_this.page_handle_model == 'forget_pwd') {
				data.type = 'forget';
			}
			_this.send_code_flag = true;
			let response = await _this.com_getdata({
				url: '/sms/send',
				data,
			});
			_this.RemainTime();
			_this.com_loading_fun({
				mode: 2,
				k: 'send_req',
				title: '已发送',
				hide_time: 300,
				response,
				suc_icon: 'success',
				fail_icon: 'none',
				show_fail_msg: true,
				suc_callback: data => {
					return () => {
						// console.log(data);
					};
				},
				fail_callback: data => {},
			});
		},

		// 已登录调用组件的修改密码等操作
		call_chang_model(model) {
			if (!model) return;
			let _this = this;
			_this.page_render = false;
			_this.$nextTick(() => {
				_this.page_model = 'section';
				_this.chang_model(model);
			});
		},

		chang_model(model) {
			let _this = this;
			this.page_handle_model = model;
			_this.reset_form(model);
		},
		topback() {
			let _this = this;
			let model = this.page_handle_model;

			// com_page_back

			switch (model) {
				case 'set_pwd':
					_this.com_page_back();
					break;
				case 'edit_pwd':
					// _this.page_model = 'page';
					_this.com_page_back();
					// _this.chang_model('yzm_login');
					break;
				case 'forget_pwd':
					_this.page_handle_model = 'pwd_login';
					// _this.com_page_back();
					break;

				case 'wechat_login':
					_this.com_page_back();
					break;
			}
		},
		btn_click() {
			let _this = this;
			let mode = _this.page_handle_model;
			// _this.loading = 1;
			switch (mode) {
				case 'set_pwd':
					_this.set_pwd();
				case 'edit_pwd':
					_this.edit_pwd();
					break;
				case 'forget_pwd':
					// _this.page_handle_model = 'pwd_login'
					_this.forget_pwd();
					break;
				default:
					_this.login_now();
					break;
			}
		},

		set_pwd() {
			this.reset_status();
		},
		edit_pwd() {
			this.reset_status();
		},
		forget_pwd() {
			let _this = this;
			if (!_this.page_check(_this.login_form.phone, 'phone')) return;
			if (!_this.page_check(_this.login_form.code, 'code')) return;
			if (!_this.page_check(_this.login_form.pwd, 'pwd')) return;
			if (!_this.page_check(_this.login_form.con_pwd, 'con_pwd')) return;
			if (_this.login_form.pwd != _this.login_form.con_pwd) {
				return _this.com_msg(_this.COMMON_VAR.page_check_config.contrast_pwd.tips_fail);
			}
			if (!_this.com_loading({ status: 1, title: '修改中...' })) return;

			let data = {
				phone: _this.login_form.phone,
				code: _this.login_form.code,
				pwd: _this.login_form.pwd,
			};
			data = _this.data_handle(data);
			uni.request({
				header: this.headers,
				url: this.apiUrl + this.apiFilename + '/forget/change',
				method: 'POST',
				data,
				success: res => {
					_this.com_loading({
						status: 2,
						title: '修改成功',
					});

					let data = res.data;
					console.log(JSON.stringify(res), 'forget_pwd');
					if (data.state == 1) {
						_this.clear_user();
						setTimeout(() => {
							_this.chang_model('pwd_login');
						}, 1000);
					}

					_this.reset_status();
				},
				fail: res => {
					uni.hideLoading();
					// _this.com_msg('网络请求错误');
				},
			});
		},

		reset_form(model) {
			let _this = this;
			switch (model) {
				case 'yzm_login':
					_this.login_form.pwd = '';
					_this.login_form.code = '';
					break;
				case 'pwd_login':
					_this.login_form.code = '';
					_this.login_form.pwd = '';
					break;
			}
		},

		getPhoneNumber(e) {
			let _this = this;

			wx.login({
				success(res) {
					_this.wx_code = res.code;
				},
			});

			// console.log(e.detail.code)  // 动态令牌
			// console.log(e.detail.errMsg) // 回调信息（成功失败都会返回）
			// console.log(e.detail.errno)  // 错误码（失败时返回）
			if (!_this.check_ystk_fun()) return;
			if (e.detail.code) {
				//新版
				// this.w_login1();
				// _this.page_step = 2;
				_this.com_getdata({
					url: '/login/phone',
					data: {
						jscode: e.detail.code,
					},
					data_handle_on: false,
					method: 'GET',
					data_fun: function (res) {
						if (res.state == 1) {
							// _this.page_step = 2;
							_this.login_form.phone = res.data;

							_this.login_now();
						} else {
							// _this.com_msg(res.msg)
						}
					},
				});
			} else {
				_this.com_msg(e.detail.errMsg);
				console.error(e.detail.errno, e.detail.errMsg);
			}
		},
		async onChooseAvatar(e) {
			let _this = this;
			let path = e.detail.avatarUrl;
			_this.avatarUrl = path;
			// console.log(e, 'onChooseAvatar')
			let { data = false } = await _this.com_upload({
				type: 'head',
				path,
			});
			if (data) {
				_this.avatarUrl = data;
			}
		},

		async login_now() {
			let _this = this;
			if (!_this.check_ystk_fun()) return;
			if (!_this.page_check(_this.login_form.phone, 'phone')) return;
			if (!_this.com_loading({ status: 1, k: 'nextreq', title: '登录中...', throttle: true })) return;
			let model = _this.page_handle_model;
			let data = {};
			let data_handle_on = true;
			let login_api = '/login/phone';
			let method = 'POST';
			switch (model) {
				case 'yzm_login':
					if (!_this.page_check(_this.send_code_flag, 'send_code_flag')) return;
					if (!_this.page_check(_this.login_form.code, 'code')) return;

					data = {
						phone: _this.login_form.phone,
						code: _this.login_form.code,
						jg_registrationid: uni.getStorageSync('registerID'),
					};
					break;
				case 'pwd_login':
					if (!_this.page_check(_this.login_form.pwd, 'pwd')) return;
					data = {
						phone: _this.login_form.phone,
						pwd: _this.login_form.pwd,
					};

					login_api = '/login/login';
					break;

				case 'wechat_login':
					data = {
						jscode: _this.wx_code,
						phone: _this.login_form.phone,
					};
					data_handle_on = false;
					method = 'GET';
					login_api = '/login/login';
					break;
			}
			let response = await _this.com_getdata({
				url: login_api,
				method,
				data,
				data_handle_on,
			});
			_this.com_loading_fun({
				mode: 2,
				k: 'nextreq',
				title: '',
				hide_time: 300,
				response,
				suc_icon: 'none',
				fail_icon: 'none',
				show_fail_msg: true,
				suc_callback: data => {
					return async () => {
						_this.reset_status();
						if (model == 'wechat_login') {
							// 微信登陆模式，如果没头像 page_step 则改为2 有头像直接登陆
							_this.set_user(data, 'login');
							let { nickname, user_head } = data.model;
							_this.nickname = nickname;
							_this.avatarUrl = user_head;
							if (!user_head) {
								_this.page_step = 2;
								return;
							}
						}
						_this.set_user(data, 'login');
						if (model == 'yzm_login') {
							await _this.get_mycarlist();
						}
						_this.page_model = 'page';
						_this.$emit('loginready');
					};
				},
				fail_callback: data => {
					_this.login_form.code = '';
					_this.clear_user();
				},
			});
		},

		// 更新用户信息
		update_user() {
			let _this = this;
			let avatarUrl = _this.avatarUrl;
			let nickname = _this.nickname;

			if (!_this.page_check(avatarUrl, 'head')) return;
			if (!_this.page_check(nickname, 'nickname')) return;
			if (!_this.com_loading({ status: 1 })) return;
			_this.com_getdata({
				url: '/user/edit',
				id_name: 'user_id',
				data: { avatarUrl, nickname },
				data_fun: function (v) {
					_this.com_loading({
						status: 0,
					});
					if (v.state == 1) {
						_this.page_model = 'page';
						_this.$emit('loginready');
					} else {
						_this.com_msg(v.msg || '网络请求错误');
					}
				},
			});
		},
		// 清除倒计时任务
		reset_status() {
			this.loading = 0;
			// this.clear_task(this.iTime_task)
			this.RemainTime(true);
		},
		showrule(type) {
			let _this = this;
			_this.goweburl_f(`/pagesC/index/com_article?id=${type}`);
		},

		initLoginTime() {
			if (uni.getStorageSync('loginTime')) {
				var oldTime = uni.getStorageSync('loginTime');
				var newTime = new Date().getTime();
				var difftime = (newTime - newTime) / 1000; //计算时间差,并把毫秒转换成秒
				var days = parseInt(difftime / 86400); // 天  24*60*60*1000
				if (days > 7) {
					return false;
				} else {
					return true;
				}
			} else {
				return false;
			}
		},
	},
	watch: {},
};
</script>

<style lang="scss">
.login_section {
	position: relative;
	// min-height: 100vh;
	// background: #4d4d88;
	overflow: hidden;

	// background: url(http://file.qijiniu.com/banner/202403/20240320165059534.jpg) no-repeat fixed center;
}

.bind_adf_swiper_view {
	position: relative;
	height: 100vh;

	.bind_btn {
		position: absolute;
		left: 50%;
		bottom: 90rpx;
		width: 600rpx;
		height: 100rpx;
		transform: translateX(-50%);
		border-radius: 50rpx;
		background-color: #1fd1c2;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 32rpx;
		color: #fff;
	}
}

.page_login {
	// padding: 0 75rpx;
	background-color: #2e3443;
	min-height: 100vh;

	&.wechat {
		background-color: #ffffff;
	}

	.login_main {
		margin: 70rpx 75rpx 0;

		position: relative;
		z-index: 5;
		// padding-top: 40rpx;
	}

	.page_logo {
		// padding-top: 80rpx;
		margin: 0 auto 114rpx;
		width: 166rpx;
		height: 180rpx;

		.img {
			width: 100%;
			height: 100%;
		}
	}

	.login_form {
		.login_form_top {
			font-size: 42rpx;
			color: #fff;
			font-weight: 700;
			margin-bottom: 30rpx;
		}

		.login_form_view {
			margin-bottom: 66rpx;

			.form_inp {
				display: flex;
				align-items: center;
				font-size: 28rpx;
				padding: 0 20rpx;
				// color: #fff;
				color: #000;
				background: #fff;
				height: 108rpx;
				// background: transparent;
				border-radius: 30rpx;
				// border: 2rpx solid #EAEDF2;

				& + .form_inp {
					margin-top: 52rpx;
				}

				.area_code {
					flex-shrink: 0;
					color: #999;
					display: flex;
					align-items: center;
					height: 100%;
					line-height: 1;
				}

				.pwd_icon {
					width: 40rpx;
					flex-shrink: 0;

					display: flex;
					align-items: center;

					.img {
						width: 40rpx;
						height: 40rpx;
					}
				}

				.inp {
					flex: 1;
					padding: 0 20rpx;
					height: 100%;
				}

				.send_code {
					flex-shrink: 0;
					color: #1fd1c2;
				}
			}
		}
	}

	.login_btn {
		background-color: #1fd1c2;
		color: #fff;
		height: 100rpx;
		border-radius: 50rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 32rpx;
		margin-bottom: 40rpx;
	}

	.btn_tips {
		display: flex;
		align-items: center;
		justify-content: center;

		&.isbet {
			justify-content: space-between;
		}

		.tips_tt {
			font-size: 28rpx;
			color: #fff;
		}
	}

	.check_ystk {
		position: fixed;
		left: 0;
		bottom: 70rpx;
		padding: 0 75rpx;
		width: 100%;

		.ystk_view {
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 24rpx;
			font-weight: 400;
			color: #fff;

			.txt_show {
				margin-left: 8rpx;
			}

			.b {
				color: #1fd1c2;
			}
		}
	}

	.page_main {
		height: 860rpx;

		.btns {
			padding-top: 70rpx;

			&.btns2 {
				padding-top: 140rpx;

				.btn {
					margin: 0;

					&:last-child {
						margin-left: 24rpx;
					}
				}
			}

			.btn {
				width: 276rpx;
				height: 100rpx;
				background: #eeeeee;
				border-radius: 14rpx;
				padding: 0;
				margin: 20rpx auto;
				font-weight: bold;
				font-size: 32rpx;
			}
		}
	}

	.wechat_login {
		padding-top: 80rpx;
	}

	.red {
		color: #ff5656;
	}

	.wechat_step_top {
		color: #999999;
		font-size: 24rpx;

		display: flex;
		align-items: flex-end;
		justify-content: center;

		line-height: 1;

		.now_page {
			color: #000000;
			font-size: 46rpx;
			font-weight: 700;
			line-height: 0.9;
		}

		.line {
			margin: 0 10rpx;
		}
	}

	.wechat_step_center {
		width: 312rpx;
		height: 312rpx;
		margin: 120rpx auto 0;
		position: relative;
		display: flex;
		align-items: center;
		justify-content: center;

		.imgs {
			width: 100%;
			height: 100%;
			position: absolute;
			top: 0;
			left: 0;
			z-index: 0;
			display: block;
		}

		.tt {
			font-size: 40rpx;
			color: #282828;
			text-align: center;
			margin-top: -30rpx;
			position: relative;
			z-index: 2;

			.t1 {
				font-weight: 700;
			}

			.t2 {
				color: #727272;
				font-size: 24rpx;
				margin-top: 4rpx;
			}
		}
	}

	.wechat_step_avatar_box {
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;

		.wechat_step_avatar {
			width: 312rpx;
			margin: 120rpx auto 0;
			position: relative;
			border-radius: 50%;

			.avatar {
				width: 312rpx;
				height: 312rpx;
				border-radius: 50%;
			}
		}

		.avatar_tt {
			line-height: 80rpx;
			text-align: center;
			color: #000;
			width: 100%;
			font-size: 28rpx;
		}
	}

	.daninput_box {
		height: 100rpx;
		border-radius: 6rpx;
		background: #f5f5f5;
		// padding-left: 150rpx;
		position: relative;
		width: 100%;
		margin: 40rpx 10rpx 0;

		display: flex;
		align-items: center;
		// justify-content: center;

		.text-k {
			height: 40rpx;
			// left: 0rpx;
			text-align: center;
			// position: absolute;
			// top: 50%;
			// transform: translateY(-50%);
			// width: 120rpx;
			flex-shrink: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 0 20rpx;
		}

		.daninput {
			flex: 1;
			height: 100%;
			padding: 0 20rpx;
		}
	}

	.wechat_step_btns {
		margin-top: 120rpx;
		display: flex;
		align-items: center;
		justify-content: center;

		// &.btns2 {
		// 	padding-top: 140rpx;

		// 	.btn {
		// 		margin: 0;

		// 		&:last-child {
		// 			margin-left: 24rpx;
		// 		}
		// 	}
		// }

		.btn {
			// width: 276rpx;
			flex: 1;
			height: 100rpx;
			background: #eeeeee;
			color: #000000;
			border-radius: 14rpx;
			padding: 0;
			margin: 10rpx;
			font-weight: bold;
			font-size: 32rpx;

			display: flex;
			align-items: center;
			justify-content: center;

			&:nth-child(2) {
				background: var(--color1);
				color: #ffffff;
			}
		}
	}

	.page_1 {
		width: 100%;

		.text1 {
			font-size: 46rpx;
		}
	}

	.page_2 {
		.edit-userinfo {
			padding: 24rpx 0;
			width: 100%;

			.daninput {
				height: 100rpx;
			}

			.upbtn {
				width: 300rpx;

				margin: 0 auto 30rpx;
				padding: 0;
				position: relative;
				border: none;
				background: none;

				&:after {
					display: none;
				}

				.textb {
					line-height: 80rpx;
					text-align: center;

					color: #000;
					width: 100%;

					font-size: 28rpx;
				}

				image {
					width: 300rpx;
					height: 300rpx;
					border-radius: 100%;
				}
			}
		}
	}
}
</style>
